/**
 * Created by wzh on 2016/8/8.
 */
swan_pc
    .factory("PersonalRole",function($resource){
        return $resource('/api/orgAuthority/my',{},{
            'queryOne': {method: 'GET'}
        })
    })
    .factory("OrgList",function($resource){
        return $resource('/api/my/organizations', {
            'query': {method: 'GET'},
        })
    })
    .factory("Member",function($resource){
        return $resource('/api/organizations/:orgid/members',{orgid:'@orgid'}, {
            'query': {method: 'GET',isArray:true}
        })
    })
    .factory("EntityOwner",function($resource){
        return $resource('/api/members/:mid/entityOwners',{mid:'@mid'}, {
            'query': {method: 'GET',isArray:true}
        })
    })
    .controller("ChangeOrgController",function($scope,$rootScope,PersonalRole,OrgList,Auth,$cookieStore,Member,$state){
        //初始化组织列表
        $scope.loadAll=function(){
            OrgList.query(function (result) {
                $scope.allOrgs=result;
                $rootScope.organization=$cookieStore.get("_currentOrganization");
                $scope.user=Auth.getUser();
                $rootScope.user=Auth.getUser();
                $rootScope.role=$scope.user.data.login=='wzh'?-1:0;
            })
        }
        $scope.loadAll();
        //切换组织
        $scope.changeOrg=function(item){
            $cookieStore.put('_currentOrganizationId', item ? item.id : null);
            $cookieStore.put('_currentOrganization', item ? item : null);
            $rootScope.organization=item;
            $rootScope.organizationId=item.id;
            $scope.user=Auth.getUser();
            PersonalRole.queryOne(function(result){
                $rootScope.role=result.roleCode;
                console.log($rootScope.role)
            },function(){
                $rootScope.role=$scope.user.data.login=='wzh'?-1:0;
            })
            Member.query({orgid:item.id},function(result){
                _.forEach(result,function(item){
                    if (item.user&&item.user.login==$scope.user.data.login) $rootScope.presentEmployeeId=item.id
                })
                console.log($rootScope.presentEmployeeId)
            })
        }
        $scope.exitManageOrg=function(){
            $cookieStore.put('_currentOrganizationId',null);
            $cookieStore.put('_currentOrganization',null);
            $rootScope.organization=undefined;
            $rootScope.role=undefined;
        }
        $scope.makeFriend=function(id){

        }
        $scope.exitOrg=function(){
            $rootScope.organization=undefined;
            $rootScope.role=undefined;
        }
        //选择组织加入
        $scope.joinInOrganization=function(){
            $state.go("joinOrg");
        }
    })